<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/29
 * Time: 20:46
 */
header('Content-type: text/html;charset=utf-8');
date_default_timezone_set('PRC');
set_time_limit(0);
ini_set('memory_limit', '512M');
require_once "config.php";
require_once "MyPDO.class.php";
require '../vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;


$db = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');
$_POST ? $data = $_POST : $data = $_GET;
$user = $_COOKIE['user'];
$shop = $data['shop'];

$beginToday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));//本日起始
$endToday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;//本日末尾

$spreadsheet = new Spreadsheet();

$spreadsheet->setActiveSheetIndex(0);

$spreadsheet->getActiveSheet()->setTitle('infos');


$shopList = $db->query("cooperation_shop", "`user` = '$user' AND (`shop` = '$shop' OR `taobao` = '$shop')","Row");


$spreadsheet->getActiveSheet()->setCellValue('A1', "店铺名");
$spreadsheet->getActiveSheet()->setCellValue('B1', '订单号');
$spreadsheet->getActiveSheet()->setCellValue('C1', '旺旺');
$spreadsheet->getActiveSheet()->setCellValue('D1', '价格');


if ($shopList['id']) {
    $i = 0;
    $arr = $db->query("tasklist", "`shop` = '$shop' AND `status` = '1001' AND `addTime` >= '$beginToday' AND `addTime` <= '$endToday' ");
//print_r($arr);


    foreach ($arr as $n => $val) {
        $val['coupons'] != '' ? $money = $val['couponsMoney'] : $money = $val['principal'];
        $spreadsheet->getActiveSheet()->setCellValue('A' . ($i + 2), $val['shop']);
        $spreadsheet->getActiveSheet()->setCellValue('B' . ($i + 2), "\t{$val['order']}");
        $spreadsheet->getActiveSheet()->setCellValue('C' . ($i + 2), $val['taobao']);
        $spreadsheet->getActiveSheet()->setCellValue('D' . ($i + 2), $money);
        $i++;
    }
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $shop . '.xlsx"');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
?>